package etc.com.market;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchOursProducts extends Activity implements OnItemClickListener{
    /** Called when the activity is first created. */
	private ListView lv;
	private static final int REQUEST_CODE = 10;
	ArrayList<String> arrtext = new ArrayList<String>();
	String str;
	String previous;
	String storename;
	String storeId;
    
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.searchourproduct);        
        
        TextView view = (TextView)findViewById(R.id.btnNext);  
        view.setText ("カテゴリーから選択");
        
        Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String messageReceive = extras.getString("message");
		String storeId = extras.getString("storeId");
		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();
			
			previous = str.substring(0, str.indexOf(";"));
			storename = str.substring(str.indexOf(";") + 1);
			
			TextView v = (TextView) findViewById(R.id.storename);
			v.setText(storename);
			
//			TextView v1 = (TextView) findViewById(R.id.headerprevious);
//			v1.setText(previous);
		}
        
        lv = (ListView)findViewById(R.id.listview);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(this);
        lv.setAdapter(new ImageAdapter(this));
        
        arrtext.add("鮮魚");
        arrtext.add("肉");
        arrtext.add("青果");
        arrtext.add("惣菜");
        arrtext.add("日配");
        arrtext.add("グロッサリー");  
               
    }
    
 // function use for footer button ==============================
 	public void Previous(View view) {
 		//onClickPre(view, (storename));
 		finish();
 		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
 	}
 	
 	public void Home(View view) {
 		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
 	}
 	
 	public void SearchProduct(View view) {

 	}
 	
 	public void SpecifyProduct(View view) {
 		onClickSpec(view, ("店舗詳細;"+ storename));
 	}
 	
 	public void Info(View view) {
 		onClickInfo(view, (storename));
 	}
 	
 	//search button?
 	public void Search(View view) {
 		onClick(view, ("Cetagory;"+ storename));
 	}
 	
 	public void Contact(View view) {
		onClickContact(view, storename);
	}
 	
 	  	 
    //===================================================================
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
//		Toast.makeText(this, "CHON:"+arg2, Toast.LENGTH_SHORT).show();
//		Intent intent = new Intent(this, SpecifyProduct.class);
//		startActivity(intent);
//		switch (arg2) {
//		case 0:
//			break;
//		case 1:
//			Intent intent = new Intent(this, SubproductAct.class);
//			startActivity(intent);
//			break;
//		case 2:
//			Intent MeatAct = new Intent(this, MeatAct.class);
//			startActivity(MeatAct);
//			break;
//		}
		
	}
	
	public void onClick(View view, String message) {
		Intent i = new Intent(this, RecommendAct.class);
		i.putExtra("storeId", storeId);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickPre(View view, String message) {
 		Intent i = new Intent(this, StoreTopPage.class);
 		i.putExtra("message", message);
 		// Set the request code to any code you like, you can identify the
 		// callback via this code
 		startActivity(i);
 		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
 	}
	
	public void onClickSpec(View view, String message) {
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickSearch(View view, String message) {
		Intent i = new Intent(this, SearchOursProducts.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickInfo(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("returnKey1")) {
				Toast.makeText(this, data.getExtras().getString("returnKey1"),
						Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	public class ImageAdapter extends BaseAdapter{
    	private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return arrtext.size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
        	LayoutInflater inflater=LayoutInflater.from(mContext);
        	
    		View row=inflater.inflate(R.layout.rowcheckbox, parent, false);
    		
    		TextView label=(TextView)row.findViewById(R.id.namefile);
    		
    		label.setText(arrtext.get(position));
			
    		return row;
        }
    }  
}
